﻿/*
 * Programer: JR Padfield
 * Description:
 * Version: 1
 * Date: 12/17/13 * 
 */

using Lidgren.Network;
using TheWorldLibrary.Characters;
using TheWorldLibrary.Enumeration;

namespace TheWorld.Networking
{
    class ClientTcp
    {
        // Private Constructor
        private ClientTcp() { }

        // Singleton Accessor
        static ClientTcp _instance;
        public static ClientTcp Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ClientTcp();
                return _instance;
            }
        }

        // Say Message //
        public void MapMsg(string msg)
        {
            NetOutgoingMessage tempBuffer = Network.Instance.TcpClient.CreateMessage();
            tempBuffer.Write((int)ClientPackets.CSayMsg);
            tempBuffer.Write(msg);
            Network.Instance.TcpClient.SendMessage(tempBuffer, NetDeliveryMethod.ReliableOrdered);
        }

        // Login
        // Send Login //
        public void SendLogin(string lName, string lPass)
        {
            NetOutgoingMessage tempBuffer = Network.Instance.TcpClient.CreateMessage();
            tempBuffer.Write((int)ClientPackets.CLogin);
            tempBuffer.Write(lName);
            tempBuffer.Write(lPass);
            Network.Instance.TcpClient.SendMessage(tempBuffer, NetDeliveryMethod.ReliableOrdered);
        }

        // Registration
        public void SendRegistration(string login, string password)
        {
            NetOutgoingMessage tempBuffer = Network.Instance.TcpClient.CreateMessage();
            tempBuffer.Write((int)ClientPackets.CAddAccount);
            tempBuffer.Write(login);
            tempBuffer.Write(password);
            Network.Instance.TcpClient.SendMessage(tempBuffer, NetDeliveryMethod.ReliableOrdered);
        }

        // Use Character
        public void SendUseChar(string name)
        {
            NetOutgoingMessage tempbuffer = Network.Instance.TcpClient.CreateMessage();
            tempbuffer.Write((int)ClientPackets.CUseChar);
            tempbuffer.Write(name);
            Network.Instance.TcpClient.SendMessage(tempbuffer, NetDeliveryMethod.ReliableOrdered);
        }

        // Create Character
        public void SendCreateCharacter(string accountName, Entity e, int spriteNum)
        {
            NetOutgoingMessage tempBuffer = Network.Instance.TcpClient.CreateMessage();
            tempBuffer.Write((int)ClientPackets.CAddChar);
            tempBuffer.Write(accountName);
            tempBuffer.Write(e.EntityName);
            tempBuffer.Write(spriteNum);
            tempBuffer.Write((int)e.EntityType);
            tempBuffer.Write((int)e.EnitityGender);
            Network.Instance.TcpClient.SendMessage(tempBuffer, NetDeliveryMethod.ReliableOrdered);
        }

        // Player Movement
        public void SendPlayerMove(int dir, float x, float y)
        {
            NetOutgoingMessage tempBuffer = Network.Instance.TcpClient.CreateMessage();
            tempBuffer.Write((int)ClientPackets.CPlayerMove);
            tempBuffer.Write(dir);
            tempBuffer.Write(x);
            tempBuffer.Write(y);
            Network.Instance.TcpClient.SendMessage(tempBuffer, NetDeliveryMethod.ReliableOrdered);
        }
    }
}
